//
//  NSObject+Jeask.m
//  Created by ti on 15/8/2.
//  Copyright (c) 2015年 ti. All rights reserved.


#import "NSObject+Jeask.h"

@implementation NSObject (Jeask)



-(NSString *)inquirePathWithFileName:(NSString *)fileName
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    
    return [path stringByAppendingPathComponent:fileName];
    
    
}
-(void)insertWithObjsWithArray:(NSMutableArray *)array FileName:(NSString *)fileName{
    [array writeToFile:[self inquirePathWithFileName:fileName] atomically:YES];
}

-(void)insertWithObjsWithDict:(NSDictionary *)dict FileName:(NSString *)fileName{

    [dict writeToFile:[self inquirePathWithFileName:fileName] atomically:YES];

}

-(void)insertWithObj:(id)obj FileName:(NSString *)fileName
{
    NSString *sourcePath = [self inquirePathWithFileName:fileName];
    NSMutableArray *source = [NSMutableArray arrayWithContentsOfFile:sourcePath];
    if (!source) {source = [NSMutableArray new];}
    [source addObject:obj];
    [source writeToFile:sourcePath atomically:YES];
}


-(void)insertWithDict:(NSDictionary *)dict FileName:(NSString *)fileName
{
    [self insertWithObj:dict FileName:fileName];
}

-(void)insertWithArray:(NSMutableArray *)array FileName:(NSString *)fileName
{
    [self insertWithObj:array FileName:fileName];
}

-(NSMutableArray *)inquireObjsWithFileName:(NSString *)fileName
{
    return [NSMutableArray arrayWithContentsOfFile:[self inquirePathWithFileName:fileName]];
}
-(NSDictionary*)inquireDictWithFileName:(NSString *)fileName{

    NSLog(@"文件名%@",fileName);
    return [NSDictionary dictionaryWithContentsOfFile:[self inquirePathWithFileName:fileName]];
    
    

}

-(id)inquireObjWithFileName:(NSString *)fileName AtIndex:(NSInteger)index
{
    return [[self inquireObjsWithFileName:fileName] objectAtIndex:index];
}

-(void)removeObjsWithFileName:(NSString *)fileName
{
    NSMutableArray *source = [self inquireObjsWithFileName:fileName];
    source = [NSMutableArray new];
    [source writeToFile:[self inquirePathWithFileName:fileName] atomically:YES];
}

-(void)removeObjWithFileName:(NSString *)fileName AtIndex:(NSInteger)index
{
    NSMutableArray *source = [self inquireObjsWithFileName:fileName];
    [source removeObjectAtIndex:index];
    [source writeToFile:[self inquirePathWithFileName:fileName] atomically:YES];
}

-(void)changeObjWithFileName:(NSString *)fileName FromIndex:(NSInteger)fromIndex ToIndex:(NSInteger)toIndex
{
    NSMutableArray *source = [self inquireObjsWithFileName:fileName];
    id objFrom = [source objectAtIndex:fromIndex];
    id objTo = [source objectAtIndex:toIndex];
    [source removeObjectAtIndex:toIndex];
    [source removeObjectAtIndex:fromIndex];
    [source insertObject:objTo atIndex:fromIndex];
    [source insertObject:objFrom atIndex:toIndex];
}

@end
